package com.geekaca.d05.work1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

public class Practice02 {
    public static void main(String[] args) {
        System.out.println("===========ArrayList 与 LinkedList 删除元素对比===========");
        // ArrayList 方式
        ArrayList<String> stuArrayList = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            stuArrayList.add("Tom_" + i);
        }

        /*long start1 = System.currentTimeMillis();


        for (int i = 0; i < 1000; i++) {
            Random random = new Random();

            int i1 = random.nextInt(stuArrayList.size()); // 每删一次 集合的长度会减小一个,所用随机数范园需要动态获取
//            System.out.print(i1+ " ");
            stuArrayList.remove(stuArrayList.get(i1));
        }
        System.out.println();

        long end1 = System.currentTimeMillis();

        System.out.println("ArrayList 随机删除100个元素耗时: " + (end1 - start1));*/

        // LinkedList 方式
        LinkedList<String> stuLinkedList = new LinkedList<>();
        for (int i = 0; i < 100000; i++) {
            stuLinkedList.add("Tom_" + i);
        }
        long start2 = System.currentTimeMillis();

        /*for (int i = 0; i < 1000; i++) {
            Random random = new Random();

            int i2 = random.nextInt(stuLinkedList.size());
            stuLinkedList.remove(stuLinkedList.get(i2));
        }
        System.out.println();

        long end2 = System.currentTimeMillis();

        System.out.println("LinkedList 随机删除100个元素耗时: " + (end2 - start2));

        System.out.println();*/

        System.out.println("===========ArrayList 与 LinkedList 查找元素对比===========");
        System.out.println();

        Random random = new Random();
        int i1 = random.nextInt(stuArrayList.size());
        String randName = stuArrayList.get(i1);
        System.out.printf("查找 ArrayList %s", randName);
        System.out.println();

        // ArrayList 查找元素
        long start3 = System.currentTimeMillis();

        int count1 = 0;
        for (String s : stuArrayList) {
            count1 += 1;
            if (randName.equals(s)) {
                System.out.println(s);
                break;
            }
        }

        System.out.println("count1= " + count1);



        /*for (int i = 0; i < stuLinkedList.size(); i++) {
            String item = stuArrayList.get(i);
            if(randName.equals(item)){
                System.out.println("找到了");
                break;
            }
//            System.out.println(item);
        }*/


        long end3 = System.currentTimeMillis();
        System.out.println("ArrayList 随机查找元素耗时: " + (end3 - start3));



        // LinkedList 查找元素
//        System.out.printf("查找 LinkedList %s", randName);
        System.out.println();
        System.out.printf("查找 LinkedList %s", randName);
        System.out.println();
        long start4 = System.currentTimeMillis();

        int count2 = 0;
        for (String s : stuLinkedList) {
            count2 += 1;
            if (randName.equals(s)) {
                System.out.println("找到了 " + randName);
                break;
            }
        }

        System.out.println("count= " + count2);

        /*for (int i = 0; i < stuLinkedList.size(); i++) {
            String item = stuLinkedList.get(i);
            if(randName.equals(item)){
                System.out.println("找到 了");
                break;
            }
//            System.out.println(item);
        }*/

        System.out.println();
        long end4 = System.currentTimeMillis();
        System.out.println("LinkedList 随机查找元素耗时: " + (end4 - start4));
    }
}
